using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;

/// <summary>
/// Summary description for Action
/// </summary>
public class Action
{
	public Action()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public void CreateActionDIV(ref HtmlTableCell htc,DataRow[] dractions,string targetcontrolid,string strkey,bool isheader)
    {
        if (dractions != null)
        {
            if (dractions.Length > 0)
            {
                Label label = new Label();
                //label.Width = "100%";
                string id = "div_" + targetcontrolid;
                label.ID = id;
                label.CssClass = "Grid_Row_actions";
                HtmlTable ht = new HtmlTable();
                ht = CreateContent(dractions, strkey);
                ht.Attributes.Add("class", "NotesTable"); // Added by 'Imran Gani' on 06-Mar-2014, for theme border color 
                #region Old code commented by 'Imran Gani' on 06-Mar-2014, for better UI of Action panel
                //ht.BorderColor = "GrayText";
                //ht.Style.Add("border-bottom","1px");
                //ht.Style.Add("border-top","1px");
                //ht.Style.Add("border-left","1px");
                //ht.Style.Add("border-right","1px");
                //label.BorderColor = System.Drawing.Color.FromName("GrayText");
                //label.BorderWidth = Unit.Pixel(1);
                #endregion
                label.Controls.Add(ht);
                if (!isheader)
                    label.Style.Add("display", "none");
                if (!isheader)
                    label.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                label.Attributes.Add("onmouseout", "javascript:HideDivForActions('div_" + targetcontrolid + "');");
                htc.Attributes.Add("onmouseover", "javascript:ShowDivForActions('div_" + targetcontrolid + "');");
                htc.Attributes.Add("onmouseout", "javascript:HideDivForActions('div_" + targetcontrolid + "');");
                htc.Controls.Add(label);

                #region headercode
                //if (!isheader)
                //{
                //    HoverMenuExtender hme = new HoverMenuExtender();    
                //    hme.TargetControlID = targetcontrolid;
                //    hme.PopupControlID = id;

                //    hme.PopupPosition = HoverMenuPopupPosition.Bottom;
                //    htc.Controls.Add(hme);
                //}




                ////HoverMenuExtender hme = new HoverMenuExtender();
                ////hme.TargetControlID = targetcontrolid;
                ////hme.PopupControlID = id;
                ////if (!isheader)
                ////    hme.PopupPosition = HoverMenuPopupPosition.Bottom;
                ////else
                ////{
                ////    hme.PopupPosition = HoverMenuPopupPosition.Left;
                ////    hme.OffsetX = 65;
                ////    hme.OffsetY=20;//20
                ////    //label.Style.Add("","")
                ////    label.Style.Add("position", "absolute");
                ////}

                #endregion
            }
        }
    }

    private HtmlTable CreateContent(DataRow[] drractions,string strkeyValue)
    {
        string result = "";
        string strPagesetRowId = "";
        string strMenuRowId = "";
        HtmlTable ht = new HtmlTable();
        ht.Border = 0;
        ht.Width = "100%";
        ht.CellPadding = 0;
        ht.CellSpacing = 0;
        ht.BorderColor = "GrayText";
        HtmlTableRow htr = new HtmlTableRow();
        HtmlTableCell htc = new HtmlTableCell();
        if (drractions != null)
        {
            if (drractions.Length > 0)
            {
                
               // Add header 
                htc = new HtmlTableCell();
                htc.NoWrap = true;
                Label lbl = new Label();
                lbl.Width = Unit.Percentage(100);
                lbl.Text = "<img style=\"vertical-align:bottom\" src=\"Images/action_16.gif\"></img><span style=\"vertical-align:bottom\">&nbsp;Actions</span>";
                lbl.CssClass = "controlFont pgForm_title";
                lbl.Style.Add("padding-top", "4px"); // Added by 'Imran Gani' on 06-Mar-2014, for better action UI
                lbl.Height = 20;
                htc.Controls.Add(lbl);
                htr.Controls.Add(htc);
                ht.Controls.Add(htr);
                #region extra line in table
                ////htr = new HtmlTableRow();
                ////htc = new HtmlTableCell();
                ////htc.Attributes.Add("class", "Grid_Row1");
                //////htc.Controls.Add(new LiteralControl("&nbsp;"));
                ////htc.Height = "1px";
                ////htr.Controls.Add(htc);
                ////ht.Controls.Add(htr);

                //
                #endregion

                //Add Content
                for (int i = 0; i < drractions.Length; i++)
                {
                    //DataRow drr = GetPageSetIDFromURL(drractions[i]["target_url"].ToString()); 
                    //if(drr!=null)
                    //{
                    //    strPagesetRowId = drr["pageset_id"].ToString();
                    //    strMenuRowId = drr["af_row_id"].ToString();
                    //}
                    htr = new HtmlTableRow();
                    htc = new HtmlTableCell();
                    htc.NoWrap = true;
                    //htc.Attributes.Add("class", "controlFont Grid_Row1");
                    Label lbldata = new Label();
                    lbldata.Text = "&nbsp;&nbsp;<a href=\"" + CreateNavigateURL(HttpUtility.HtmlEncode(drractions[i]["target_url"].ToString()),strkeyValue)+"\">" + HttpUtility.HtmlEncode(drractions[i]["name"].ToString()) + "</a>&nbsp;&nbsp;";
                    //lbldata.Text = "&nbsp;&nbsp;<a href=\"" + drractions[i]["target_url"].ToString() + "?pgs_id=" + strPagesetRowId + "&PK=" + strkeyValue + "&mn="+strMenuRowId+"&VMode=1 \">" + drractions[i]["name"].ToString() + "</a>&nbsp;&nbsp;";
                    //lbldata.CssClass = "Grid_Row1";
                    lbldata.ToolTip = drractions[i]["description"].ToString();
                    lbldata.Width = Unit.Percentage(100);
                    lbldata.CssClass = "controlFont Grid_Row_actions";
                    lbldata.Style.Add("padding-top", "4px"); // Added by 'Imran Gani' on 06-Mar-2014, for better action UI
                    lbldata.Height = 20;
                    htc.Controls.Add(lbldata);
                    //lbldata = new Label(); // Commented by 'Imran Gani' on 06-Mar-2014, for better action UI
                    //lbldata.Text = "";
                    //lbldata.CssClass = "Grid_Row_actions";
                    //htc.Controls.Add(lbldata);
                    htr.Controls.Add(htc);
                    ht.Controls.Add(htr);
                }
                //
                #region ExtraLine for end
                // Add detail
                //htc = new HtmlTableCell();
                //htc.Attributes.Add("class","Grid_Row1");
                //htr = new HtmlTableRow();
                //htc.NoWrap = true;
                ////Label lblfooter = new Label();
                ////lblfooter.Text = "&nbsp;";
                ////lblfooter.CssClass = "Grid_Row1";
                ////lblfooter.Width = Unit.Percentage(100);
                ////htc.Controls.Add(lblfooter);
                //htc.NoWrap = true;
                //htr.Controls.Add(htc);
                //ht.Controls.Add(htr);
                //
                #endregion
        
            }
        }
        return ht;

    }

    //Added by srivatsn on Nov 02 2009
    //This will create a action image inside the td
    public void CreateActionImage(string id,ref HtmlTableCell htc)
    {
        try
        {
            Label lbl = new Label();
            lbl.Text = "<img style=\"vertical-align:bottom\" src=\"images/action_16.gif\"></img><span style=\"vertical-align:bottom\">&nbsp;Actions&nbsp;</span>";
            lbl.CssClass = "controlFont";
            lbl.ID = id;
            htc.Controls.Add(lbl);
            
        }
        catch
        {

        }
        finally
        {
 
        }

    }

    //Added by srivatsan to get the pageset id and menu id from the url
    private DataRow GetPageSetIDFromURL(string url)
    {
        DataRow drr = null;
        HDDatabase Hdd= new HDDatabase();
        try
        {
            if (url.Trim() != "")
            {
                string sql = "select top 1 pageset_id,af_row_id from sy_mnu where pageset_url = '" + url + "'";
                drr = Hdd.GetTableRow(sql);
            }
        }
        catch
        {
        }
        finally
        {
            Hdd = null;
        }
        return drr;
    }

    // Added by srivatsan
    // This will create URL for the target page. 
    //If any quey string exists will append the action string. else create a new url
    private string CreateNavigateURL(string url, string strID)
    {
        string tempurl = url;
        try
        {
            if (url.Trim() != "")
            {
                if (url.Contains("?"))
                    url = url + "&ID=" + strID + "&action=1";
                else
                    url = url + "?ID=" + strID + "&action=1";
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : CreateNavigateURL : " + ex.Message);

            url = tempurl + "?ID=" + strID + "&action=1";
        }

        return url;
    }
}
